﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SERP.StringMappings;
using SERP.DAO;
using SERP.Controllers;
using System.IO;
namespace SERP.Modules.People
{
    public partial class PeopleImport : SERP.Modules.ModuleBase
    {
        private PeopleController controller = new PeopleController();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadLanguage();

        }
        //Load All lable text
        private void LoadLanguage()
        {
            labelPersonCode.Text = PeopleStringMapping.PersonCode + ":";
            labelFullName.Text = PeopleStringMapping.FullName + ":";
            labelGender.Text = PeopleStringMapping.Gender + ":";
            labelBirthDay.Text = PeopleStringMapping.BirthDay + ":";
            labelAddress.Text = PeopleStringMapping.Address + ":";
            labelPhone.Text = PeopleStringMapping.Phone + ":";
            labelEmail.Text = PeopleStringMapping.Email + ":";
            labelPositionId.Text = PeopleStringMapping.PositionId + ":";
            labelStartDate.Text = PeopleStringMapping.StartDate + ":";
            labelNote.Text = PeopleStringMapping.Note + ":";
            labelOrganizationId.Text = PeopleStringMapping.OrganizationId + ":";
            labelPersonTypeId.Text = PeopleStringMapping.PersonTypeId + ":";
            lblFile.Text = Constants.APP_FILE + ":";
            lblSheet.Text = "Sheet:";
            btnUpload.Text = Constants.APP_UPLOAD;
            btnRead.Text = Constants.APP_READ;
            btnSave.Text = Constants.APP_SAVE;
            lblInput.Text = Constants.APP_FILE_INFOMATION;
            lblPreview.Text = Constants.APP_PREVIEW;
            lblTitle.InnerText = Constants.APP_IMPORT + " " + PeopleStringMapping.People;
        }

        protected void tv_SelectedNodeChanged(object sender, EventArgs e)
        {
        }
        protected void btnRead_Click(object sender, EventArgs e)
        {
            BindPeople(GetFileName(txtFileName.Value));
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            LoadPeople(GetFileName(txtFileName.Value));
            foreach (DataRow row in controller.Table.Rows)
            {
                controller.PeopleObject = controller.MapDataRowToObject(row);
                controller.AddNew();
            }
            lblInfo.Text = Constants.MSS_SUCCESS;
            mvImp.ActiveViewIndex = 1;
        }
        private void LoadPeople(string fileName)
        {
            DataTable tbl = SERP.Commons.ExcelReader.ReadExcellFile(fileName, txtSheet.SelectedValue).Tables[0];
            List<int> ls = new List<int>();
            controller.LoadAllDSPeople(ls);
            controller.Table.Columns.Add("OrganizationName");
            controller.Table.Columns.Add("PersonTypeName");
            foreach (DataRow r in tbl.Rows)
            {
                DataRow row = controller.Table.NewRow();
                if (txtAddress.SelectedIndex > 0)
                {
                    row["Address"] = r[txtAddress.SelectedValue].ToString().Trim();
                }
                if (txtBirthDay.SelectedIndex > 0)
                {
                    row["BirthDay"] = Controller.ConvertDateTime(r[txtBirthDay.SelectedValue]);
                }
                if (txtEmail.SelectedIndex > 0)
                {
                    row["Email"] = r[txtEmail.SelectedValue].ToString().Trim();
                }
                if (txtFullName.SelectedIndex > 0)
                {
                    row["FullName"] = r[txtFullName.SelectedValue].ToString().Trim();
                }
                if (txtGender.SelectedIndex > 0)
                {
                    row["Gender"] = r[txtGender.SelectedValue].ToString().Trim().ToLower() == Constants.APP_MALE.ToLower() ? 1 : 0;
                }
                if (txtNote.SelectedIndex > 0)
                {
                    row["Note"] = r[txtNote.SelectedValue].ToString().Trim();
                }
                OrganizationsEntity oOrg = null;
                if (txtOrganizationId.SelectedIndex > 0)
                {
                    oOrg = OrganizationsController.GetOrganizationByText(r[txtNote.SelectedValue].ToString().Trim());

                }
                if (oOrg == null)
                {
                    oOrg = tv.GetSelectedOrganization();
                }
                if (oOrg != null)
                {
                    row["OrganizationId"] = oOrg.OrganizationId;
                    row["OrganizationName"] = oOrg.OrgName;
                }
                else
                {
                    row["OrganizationName"] = Constants.APP_NOTSET;
                }
                if (txtPersonCode.SelectedIndex > 0)
                {
                    row["PersonCode"] = r[txtPersonCode.SelectedValue].ToString().Trim();
                }
                if (txtPersonTypeId.SelectedIndex > 0)
                {
                    PersonTypesEntity o = PersonTypesController.GetPersonTypeByText(r[txtPersonTypeId.SelectedValue].ToString().Trim());
                    if (o != null)
                    {
                        row["PersonTypeId"] = o.PersonTypeId;
                        row["PersonTypeName"] = o.TypeName;
                    }
                    else
                    {
                        row["PersonTypeId"] = 0;
                        row["PersonTypeName"] = Constants.APP_NOTSET;
                    }
                }
                else
                {
                    row["PersonTypeId"] = 0;
                    row["PersonTypeName"] = Constants.APP_NOTSET;
                }
                controller.Table.Rows.Add(row);
            }
        }
        private void BindPeople(string fileName)
        {
            LoadPeople(fileName);
            gridData.DataSource = controller.Table;
            gridData.DataBind();
        }
        private void BindSheets(string fileName)
        {
            DataTable tbl = SERP.Commons.ExcelReader.ReadExcellSheet(fileName);
            DataRow r = tbl.NewRow();
            r["TABLE_NAME"] = Constants.CHOOSE_ITEM;
            tbl.Rows.InsertAt(r, 0);
            txtSheet.DataValueField = "TABLE_NAME";
            txtSheet.DataTextField = "TABLE_NAME";
            txtSheet.DataSource = tbl;
            txtSheet.DataBind();
        }
        private void BindColumns(string fileName)
        {
            string fullName = GetFileName(txtFileName.Value);
            DataTable tbl = SERP.Commons.ExcelReader.ReadExcellColumn(fullName, txtSheet.SelectedValue);
            DataRow r = tbl.NewRow();
            r["COLUMN_NAME"] = Constants.CHOOSE_ITEM;
            tbl.Rows.InsertAt(r, 0);
            BindColumn(txtAddress, tbl);
            BindColumn(txtBirthDay, tbl);
            BindColumn(txtEmail, tbl);
            BindColumn(txtFullName, tbl);
            BindColumn(txtGender, tbl);
            BindColumn(txtNote, tbl);
            BindColumn(txtOrganizationId, tbl);
            BindColumn(txtPersonCode, tbl);
            BindColumn(txtPersonTypeId, tbl);
            BindColumn(txtPositionId, tbl);
            BindColumn(txtPhone, tbl);
            BindColumn(txtStartDate, tbl);
        }
        private void BindColumn(DropDownList txt, DataTable tbl)
        {
            txt.DataValueField = "COLUMN_NAME";
            txt.DataTextField = "COLUMN_NAME";
            txt.DataSource = tbl;
            txt.DataBind();
        }
        protected void txtSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindColumns(txtFileName.Value);
        }
        private string GetFileName(string fileName)
        {
            return Server.MapPath(GetUserFilesStore() + "/tmp/" + fileName);
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string fileName = "ImpPeople" + Path.GetExtension(txtFile.FileName);
            string fullPath = GetFileName(fileName);
            if (!Directory.Exists(Path.GetDirectoryName(fullPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
            }
            txtFileName.Value = fileName;
            txtFile.SaveAs(fullPath);
            BindSheets(fullPath);
        }
        private void GoBack()
        {
            RedirectToPage("PeopleList");
        }
        protected void OnCancel(object sender, EventArgs e)
        {
            GoBack();
        }
    }
}
